package com.example.myapplication.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.view.Gravity;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.api.HttpApiService;
import com.example.myapplication.api.MusicApiService;
import com.example.myapplication.entity.Artist;
import com.example.myapplication.entity.BaseResponse;
import com.example.myapplication.entity.Login;
import com.example.myapplication.entity.RecommendSongs;
import com.example.myapplication.tools.BarTransparent;

import java.io.IOException;
import java.util.List;

import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoadingActivity extends AppCompatActivity {
    public static String cookie;
    public static Login.ProfileBean profileBean;
    public static List<Artist> artistList;
    public static List<RecommendSongs.DataBean.DailySongsBean> recommendSongsList;
    private int code;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarTransparent.makeStatusBarTransparent(this);
        BarTransparent.setAndroidNativeLightStatusBar(this,true);
        setContentView(R.layout.loading);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        okGo();
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String username = bundle.getString("username","null");
        String password = bundle.getString("password","null");
        code = httpServer(username,password);
        if(code==200){
            loadArtistList();
            Intent i =  new Intent(LoadingActivity.this,BottomActivity.class);
            new Handler().postDelayed(new Runnable(){
                public void run(){
                    //等待1000毫秒后销毁此页面，并提示登陆成功
                    startActivity(i);
                    Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }, 1000);
        }
        else{
            Toast toast = Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            Intent i =  new Intent(LoadingActivity.this,LoginActivity.class);
            startActivity(i);
            finish();
        }
    }
    @Override
    public void onWindowFocusChanged(boolean focus) {
        super.onWindowFocusChanged(focus);
    }


    private int httpServer(String username,String password ){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        HttpApiService service =  retrofit.create(HttpApiService.class);
        Call<Login> call = service.login(username,password);
        try {
              Login login = call.execute().body();
              if(login!=null) {
                  cookie = login.getCookie();
                  profileBean = login.getProfile();
              }
              return login==null?400:login.getCode();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }


    public void okGo() {

        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isAvailable()) {
            Toast.makeText(LoadingActivity.this, "当前网络可用", Toast.LENGTH_SHORT).show();


        } else {
            Toast.makeText(LoadingActivity.this, "当前网络不可用", Toast.LENGTH_SHORT).show();
        }
    }


    private void loadArtistList(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        loadData();
        MusicApiService service =  retrofit.create(MusicApiService.class);
        Call<BaseResponse> call = service.getArtistList();
        try {
            artistList = call.execute().body().getArtists();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void loadData(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MusicApiService service =  retrofit.create(MusicApiService.class);
        Call<RecommendSongs> call = service.getRecommendSongs(LoadingActivity.cookie);
        try {
            recommendSongsList = call.execute().body().getData().getDailySongs();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}